package cn.csc.module.pay.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.task.TaskExecutionProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author liuchuang
 * @version 1.0
 * @modify
 * @since 2025/1/2 17:38
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
public class ExecutorConfiguration {

    @Bean(name = "defaultTaskExecutor")
    public ThreadPoolTaskExecutor defaultTaskExecutor(TaskExecutionProperties properties) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(properties.getPool().getCoreSize()); // 设置核心线程数
        executor.setMaxPoolSize(properties.getPool().getMaxSize()); // 设置最大线程数
        executor.setKeepAliveSeconds((int) properties.getPool().getKeepAlive().getSeconds()); // 设置空闲时间
        executor.setQueueCapacity(properties.getPool().getQueueCapacity()); // 设置队列大小
        executor.setThreadNamePrefix(properties.getThreadNamePrefix()); // 配置线程池的前缀
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 进行加载
        executor.initialize();
        return executor;
    }


}
